from django.contrib import admin
from django.utils.safestring import mark_safe

from . import models


# 后台标题
admin.site.site_title = "我的管理后台"
admin.site.site_header = "后台管理"


# 前端展示信息
class WebInfoManager(admin.ModelAdmin):
    list_display = ['name', 'avatar_image', 'birthday', 'qq', 'wx', 'github', 'gitee']

    # 显示头像图片
    def avatar_image(self, obj):
        try:
            obj_url = obj.avatar.url
        except:
            return ''
        return mark_safe('<img src="{url}" width="{width}" height={height} />'
                    .format(url=obj_url, width='70px', height='70px', )
                 )
    avatar_image.short_description = mark_safe('<div style="color:#409eff">头像<div>')

    # 只允许添加一个对象
    def has_add_permission(self, request):
        if self.model.objects.count() >= 1:
            return False
        return super().has_add_permission(request)


admin.site.register(models.MyInfo, WebInfoManager)
